﻿using System;
using System.Windows.Forms;
using Business.Dicionary;
using Business.Entity.Contabil.Pessoa;
using Business.Entity.Contabil.Pessoa.SubClass.Cliente;
using Business.Entity.Contabil.Pessoa.SubClass.Usuario.ClassesRelacionadas;
using Business.Forms;
using Business.Seguranca;
using Business.Validation;
using DevExpress.XtraEditors;

namespace Bonesoft.Forms.Contabil
{
    public partial class FormCliente : FormDefault, IForm
    {
        
        public FormCliente()
        {
            InitializeComponent();
            
            enderecoEletronicoListControl.FormEdit = new FormEnderecoEletronico();
            enderecoListControl.FormEdit = new FormEndereco();
            telefoneListControl.FormEdit = new FormTelefone();

            // Carrega os dicionarys
            tipoPessoaDicionaryBindingSource.DataSource = new TipoPessoaDicionary();
            sexoDicionaryBindingSource.DataSource = new SexoDicionary();
            InitFocusControl = cpfCnpjTextEdit;
        }

        public object Novo()
        {
            var p = new Cliente();
            CurrentObject = p;
            clienteBindingSource.DataSource = p;
            ShowDialog();
            if (p.Id == 0)
            {
                return null;
            }
            return p;
        }

        public void Alterar(object entity)
        {
            CurrentObject = entity;
            clienteBindingSource.DataSource = entity;
            ShowDialog();
        }

        public object Save()
        {
            if (!Validate().HasError)
            {
                var cl = (Cliente) CurrentObject;
                PessoaRepository.Session = ClienteRepository.GetSession();
                PessoaRepository.Save(cl.Pessoa);
                ClienteRepository.Save(cl);
            }
            return null;
        }

        public void Update(object entity)
        {
            Alterar(entity);
        }

        public bool Delete(object entity)
        {
            CurrentObject = entity;
            ClienteRepository.Delete((Cliente)entity);
            return true;
        }

        public object CurrentObject
        {
            get
            {
                clienteBindingSource.EndEdit();
                return clienteBindingSource.DataSource;
            }
            set
            {
                clienteBindingSource.DataSource = value;
            }
        }

        public PermissaoForm Permissao
        {
            get
            {
                return SegurancaUtils.GetPermissaoForm(Name);
            }
        }

        public EntityValidationResult Validate()
        {
            var cliente = (Cliente)CurrentObject;
            var result = DataValidation.ValidateEntity((Cliente)CurrentObject);
            foreach (var error in DataValidation.ValidateEntity(cliente.Pessoa).ValidationErrors)
            {
                result.ValidationErrors.Add(error);
            }
            return result;
        }

        public void Reload()
        {
            
        }

        public void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        public Object New()
        {
            return Novo();
        }

        public XtraForm Form
        {
            get
            {
                return this;
            }
        }

        public FormState FormState { get; set; }


        private void cpfCnpjTextEdit_Leave(object sender, System.EventArgs e)
        {
            var cliente = ClienteRepository.GetUniqueByCpfCnpj(cpfCnpjTextEdit.Text);
            if (cliente != null)
            {
                MessageBox.Show("Já existe um fornecedor cadastrado com esse CPF/CNPJ.");

                return;
            }
            PessoaRepository.Session = ClienteRepository.GetSession();
            var pessoa = PessoaRepository.GetUniqueByCpfCnpj(cpfCnpjTextEdit.Text);
            if (pessoa != null)
            {

                ((Cliente)clienteBindingSource.Current).Pessoa = pessoa;
            }

        }
    }
}